package net.pws.oos.web.dto;

import java.util.Date;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Dict;
import net.pws.oos.biz.model.IndicatorTemplate;
import net.pws.oos.biz.model.IndicatorValue;
import net.pws.oos.biz.model.Organization;
import net.pws.oos.biz.model.User;
import net.pws.oos.security.SecurityUtils;

import org.apache.commons.lang.StringUtils;

public class IndicatorTemplateDto extends IndicatorTemplate {

	private static final long serialVersionUID = 6440432477851004163L;

	public static IndicatorTemplateDto from(IndicatorTemplate indicatorTemplate) {
        IndicatorTemplateDto result = new IndicatorTemplateDto();
        String[] props = {"special", "creator", "constructionModel","createOrganization"};
        BeanUtils.copyProperties(result, indicatorTemplate, props);
        Dict special = indicatorTemplate.getSpecial();
        if (null != special) {
        	result.setSpecialId(special.getId());
        	result.setSpecialName(special.getName());
        }
        IndicatorValue constructionModel = indicatorTemplate.getConstructionModel();
        if (null != constructionModel) {
        	result.setConstructionModelId(constructionModel.getId());
        	result.setConstructionModelName(constructionModel.getName());
        }
        User creator = indicatorTemplate.getCreator();
        if (null != creator) {
            result.setCreatorId(creator.getId());
            result.setCreatorName(creator.getName());
        }
//        Organization createOrganization = indicatorTemplate.getCreateOrganization();
//        if (null != createOrganization) {
//            result.setCreateOrganId(createOrganization.getId());
//            result.setCreateOrganName(createOrganization.getName());
//        }
        return result;
    }
    
    public static IndicatorTemplate to(IndicatorTemplateDto dto, EntityManager entityManager) {
        IndicatorTemplate result = new IndicatorTemplate();
        if (!StringUtils.isEmpty(dto.getId())) {
            result = entityManager.find(IndicatorTemplate.class, dto.getId());
            
            if (null == result) {
                throw new RuntimeException("无效的模板id");
            }
        }
        
        result.setName(dto.getName());

        result.setDescription(dto.getDescription());
        
		if (StringUtils.isEmpty(dto.getSpecialId())) {
			result.setSpecial(null);   	
		} else {
			result.setSpecial(entityManager.find(Dict.class, dto.getSpecialId()));
		}
		
		if (StringUtils.isEmpty(dto.getConstructionModelId())) {
			result.setConstructionModel(null);	
		} else {
			result.setConstructionModel(entityManager.find(IndicatorValue.class, dto.getConstructionModelId()));
		}
		User creator;
        if (StringUtils.isEmpty(dto.getCreatorId())) {        	
        	creator = entityManager.find(User.class, SecurityUtils.currentUserId());
		} else {
			creator = entityManager.find(User.class, dto.getCreatorId());
		}
        result.setCreator(creator);        	
        
        if (StringUtils.isEmpty(dto.getCreateOrganId())) {
        	result.setCreateOrganization(null);   	
		} else {
			result.setCreateOrganization(entityManager.find(Organization.class, dto.getCreateOrganId()));
		}
        
        if (result.getCreateOrganization() == null && result.getCreator() != null) {
        	result.setCreateOrganization(creator.getOrganization());
		}

        if (result.getCreateDateTime() == null) {
        	result.setCreateDateTime(new Date());
        }
        
        return result;
    }
    
    private String specialId;
    
    private String specialName;
    
    private String creatorId;
    
    private String creatorName;
    
    private String createOrganId;
    
    private String createOrganName;
    
    private String constructionModelId;
    
    private String constructionModelName;
    
	public String getSpecialId() {
		return specialId;
	}

	public void setSpecialId(String specialId) {
		this.specialId = specialId;
	}

	public String getSpecialName() {
		return specialName;
	}

	public void setSpecialName(String specialName) {
		this.specialName = specialName;
	}

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getCreateOrganId() {
		return createOrganId;
	}

	public void setCreateOrganId(String createOrganId) {
		this.createOrganId = createOrganId;
	}

	public String getCreateOrganName() {
		return createOrganName;
	}

	public void setCreateOrganName(String createOrganName) {
		this.createOrganName = createOrganName;
	}

	public String getConstructionModelId() {
		return constructionModelId;
	}

	public void setConstructionModelId(String constructionModelId) {
		this.constructionModelId = constructionModelId;
	}

	public String getConstructionModelName() {
		return constructionModelName;
	}

	public void setConstructionModelName(String constructionModelName) {
		this.constructionModelName = constructionModelName;
	}	
    
}
